package liy.demo.wikiproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import liy.demo.wikiproject.domain.Ebook;
import liy.demo.wikiproject.mapper.EbookMapper;
import liy.demo.wikiproject.req.EbookQueryReq;
import liy.demo.wikiproject.req.EbookSaveReq;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.EbookResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.util.CopyUtil;
import liy.demo.wikiproject.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Slf4j
public class EBookService {

    @Resource
    EbookMapper EbookMapper;

    @Resource
    SnowFlake snowFlake;

    public PageResp<EbookResp> list( EbookQueryReq req) {
        if (req.getSize() != null) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }
        List<Ebook> ebookList;
        if (req.getName() != null && !req.getName().isEmpty()) {
            ebookList = EbookMapper.findByName("%" + req.getName() + "%");
        } else {
            ebookList = EbookMapper.selectAll();
        }
        List<EbookResp> respList = CopyUtil.copyList(ebookList, EbookResp.class);
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        PageResp<EbookResp> pageResp = new PageResp<>();
        pageResp.setTotal((int) pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }

    public PageResp<EbookResp> listByCategory( EbookQueryReq req) {
        if (req.getSize() != null) {
            PageHelper.startPage(req.getPage(), req.getSize());
        }
        List<Ebook> ebookList=null;
        if (req.getCategory2Id() != null) {
            ebookList = EbookMapper.findByCategoryId(req.getCategory2Id());
        } else{
            throw new RuntimeException("没有输入分类目录ID啊哥哥！！！");
        }
        List<EbookResp> respList = CopyUtil.copyList(ebookList, EbookResp.class);
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        PageResp<EbookResp> pageResp = new PageResp<>();
        pageResp.setTotal((int) pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }
    public CommonResp<String> delete(EbookQueryReq req) {
        long id = req.getId();
        CommonResp<String>commonResp = new CommonResp<>();

        try {
            EbookMapper.deleteByPrimaryKey(id);
            commonResp.setContent("删除成功");
        }catch (Exception e) {
            commonResp.setContent("删除失败");
        }finally {
            return commonResp;
        }
    }
    public void save(EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req,Ebook.class);
        if(ObjectUtils.isEmpty(req.getId())) {
            long l = snowFlake.nextId();
            ebook.setId(l);
            EbookMapper.insert(ebook);
        }else{
            EbookMapper.updateByPrimaryKey(ebook);

        }
    }
}
